package com.uroad.jilinpda.widget;

import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TableRow;

import com.uroad.jilinpda.R;
import com.uroad.lib.util.sys.SystemUtil;


public class SelectProvincePop extends PopupWindow {

	Context mContext;
	int screenWidth = 0, screenHeight = 0;
	onSelectProvince selectProvince;

	public SelectProvincePop(Context mCon) {
		mContext = mCon;
		init();
	}

	private void init() {
		// pop = new PopupWindow(LayoutParams.MATCH_PARENT,
		// LayoutParams.WRAP_CONTENT);
		View view = LayoutInflater.from(mContext).inflate(
				R.layout.pop_province_select, null, false);

		screenWidth = SystemUtil.getScreenWidth((Activity) mContext);
		screenHeight = SystemUtil.getScreenHeight((Activity) mContext);
		Button btn1 = (Button) view.findViewById(R.id.btn1);

		Button btn2 = (Button) view.findViewById(R.id.btn2);
		Button btn3 = (Button) view.findViewById(R.id.btn3);
		Button btn4 = (Button) view.findViewById(R.id.btn4);
		Button btn5 = (Button) view.findViewById(R.id.btn5);
		Button btn6 = (Button) view.findViewById(R.id.btn6);
		Button btn7 = (Button) view.findViewById(R.id.btn7);
		Button btn8 = (Button) view.findViewById(R.id.btn8);
		Button btn9 = (Button) view.findViewById(R.id.btn9);
		Button btn10 = (Button) view.findViewById(R.id.btn10);
		Button btn11 = (Button) view.findViewById(R.id.btn11);
		Button btn12 = (Button) view.findViewById(R.id.btn12);
		Button btn13 = (Button) view.findViewById(R.id.btn13);
		Button btn14 = (Button) view.findViewById(R.id.btn14);
		Button btn15 = (Button) view.findViewById(R.id.btn15);
		Button btn16 = (Button) view.findViewById(R.id.btn16);
		Button btn17 = (Button) view.findViewById(R.id.btn17);
		Button btn18 = (Button) view.findViewById(R.id.btn18);
		Button btn19 = (Button) view.findViewById(R.id.btn19);
		Button btn20 = (Button) view.findViewById(R.id.btn20);
		Button btn21 = (Button) view.findViewById(R.id.btn21);
		Button btn22 = (Button) view.findViewById(R.id.btn22);
		Button btn23 = (Button) view.findViewById(R.id.btn23);
		Button btn24 = (Button) view.findViewById(R.id.btn24);
		Button btn25 = (Button) view.findViewById(R.id.btn25);
		Button btn26 = (Button) view.findViewById(R.id.btn26);
		Button btn27 = (Button) view.findViewById(R.id.btn27);
		Button btn28 = (Button) view.findViewById(R.id.btn28);
		Button btn29 = (Button) view.findViewById(R.id.btn29);
		Button btn30 = (Button) view.findViewById(R.id.btn30);
		Button btn31 = (Button) view.findViewById(R.id.btn31);
		Button btnCancel = (Button) view.findViewById(R.id.btnCancel);
		btn1.setTag(btn1.getText());
		btn2.setTag(btn2.getText());
		btn3.setTag(btn3.getText());
		btn4.setTag(btn4.getText());
		btn5.setTag(btn5.getText());
		btn6.setTag(btn6.getText());
		btn7.setTag(btn7.getText());
		btn8.setTag(btn8.getText());
		btn9.setTag(btn9.getText());
		btn10.setTag(btn10.getText());
		btn11.setTag(btn11.getText());
		btn12.setTag(btn12.getText());
		btn13.setTag(btn13.getText());
		btn14.setTag(btn14.getText());
		btn15.setTag(btn15.getText());
		btn16.setTag(btn16.getText());
		btn17.setTag(btn17.getText());
		btn18.setTag(btn18.getText());
		btn19.setTag(btn19.getText());
		btn20.setTag(btn20.getText());
		btn21.setTag(btn21.getText());
		btn22.setTag(btn22.getText());
		btn23.setTag(btn23.getText());
		btn24.setTag(btn24.getText());
		btn25.setTag(btn25.getText());
		btn26.setTag(btn26.getText());
		btn27.setTag(btn27.getText());
		btn28.setTag(btn28.getText());

		btn29.setTag(btn29.getText());
		btn30.setTag(btn30.getText());
		btn31.setTag(btn31.getText());

		adjust(btn1);
		adjust(btn2);
		adjust(btn3);
		adjust(btn4);
		adjust(btn5);
		adjust(btn6);
		adjust(btn7);
		adjust(btn8);
		adjust(btn9);
		adjust(btn10);
		adjust(btn11);
		adjust(btn12);
		adjust(btn13);
		adjust(btn14);
		adjust(btn15);
		adjust(btn16);
		adjust(btn17);
		adjust(btn18);
		adjust(btn19);
		adjust(btn20);
		adjust(btn21);
		adjust(btn22);
		adjust(btn23);
		adjust(btn24);
		adjust(btn25);
		adjust(btn26);
		adjust(btn27);
		adjust(btn28);
		adjust(btn29);
		adjust(btn30);
		adjust(btn31);

		btn1.setOnClickListener(clickListener1);
		btn2.setOnClickListener(clickListener1);
		btn3.setOnClickListener(clickListener1);
		btn4.setOnClickListener(clickListener1);
		btn5.setOnClickListener(clickListener1);
		btn6.setOnClickListener(clickListener1);
		btn7.setOnClickListener(clickListener1);
		btn8.setOnClickListener(clickListener1);
		btn9.setOnClickListener(clickListener1);
		btn10.setOnClickListener(clickListener1);
		btn11.setOnClickListener(clickListener1);
		btn12.setOnClickListener(clickListener1);
		btn13.setOnClickListener(clickListener1);
		btn14.setOnClickListener(clickListener1);
		btn15.setOnClickListener(clickListener1);
		btn16.setOnClickListener(clickListener1);
		btn17.setOnClickListener(clickListener1);
		btn18.setOnClickListener(clickListener1);
		btn19.setOnClickListener(clickListener1);
		btn20.setOnClickListener(clickListener1);
		btn21.setOnClickListener(clickListener1);
		btn22.setOnClickListener(clickListener1);
		btn23.setOnClickListener(clickListener1);
		btn24.setOnClickListener(clickListener1);
		btn25.setOnClickListener(clickListener1);
		btn26.setOnClickListener(clickListener1);
		btn27.setOnClickListener(clickListener1);
		btn28.setOnClickListener(clickListener1);
		btn29.setOnClickListener(clickListener1);
		btn30.setOnClickListener(clickListener1);
		btn31.setOnClickListener(clickListener1);
		btnCancel.setOnClickListener(clickListener1);

		setWidth(screenWidth);
		setHeight(screenHeight / 2);
		setContentView(view);
		setTouchable(true);
		setOutsideTouchable(false);

	}

	public void setSelectProvince(onSelectProvince selectProvince) {
		this.selectProvince = selectProvince;
	}

	private OnClickListener clickListener1 = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if (v.getId() == R.id.btnCancel) {
				selectProvince.onCancel();
				dismiss();
			} else {
				String text = v.getTag().toString();
				dismiss();
				selectProvince.onSelect(text);
			}
		}
	};

	public void showPopWindow(View view) {
		showAtLocation(view, Gravity.BOTTOM, 0, 0);
	}

	private void adjust(Button btn) {

		TableRow.LayoutParams params = (TableRow.LayoutParams) btn
				.getLayoutParams();
		params.width = screenWidth / 9;
		params.height = params.width;
	}

	public interface onSelectProvince {
		void onCancel();

		void onSelect(String province);
	}
}
